package com.trinity.web.controller.publicAPI;

import com.trinity.campus.domain.CampusStudents;
import com.trinity.campus.service.ICampusStudentsService;
import com.trinity.common.annotation.Anonymous;
import com.trinity.common.core.domain.AjaxResult;
import com.trinity.common.core.domain.TreeSelect;
import com.trinity.common.core.domain.entity.SysDept;
import com.trinity.system.service.ISysDeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@Anonymous
@RequestMapping("/public/stu")
public class StudentQueryApi {
    @Resource
    private ICampusStudentsService studentsService;

    @Resource
    private ISysDeptService deptService;

    /**
     * 学生查询学号接口
     *
     * @param queryStudent 姓名+班级
     * @return 学生信息
     */
    @RequestMapping(value = "/query/list", method = {RequestMethod.GET})
    public AjaxResult queryStudent(CampusStudents queryStudent) {
        List<CampusStudents> list = studentsService.selectStudentListByNameAndDept(queryStudent);
        List<Map<String, String>> stuVos = new ArrayList<>();
        for (CampusStudents stu : list) {
            Map<String, String> stuVo = new HashMap<>();
            stuVo.put("school", stu.getSchoolName());
            stuVo.put("college", stu.getCollege());
            stuVo.put("major", stu.getMajor());
            stuVo.put("clazz", stu.getClazz());
            stuVo.put("studentName", stu.getStudentName());
            stuVo.put("stuNo", stu.getStuNo());
            if (stu.getSex().equals("0")) {
                stuVo.put("sex", "男");
            } else if (stu.getSex().equals("1")) {
                stuVo.put("sex", "女");
            } else {
                stuVo.put("sex", "未知");
            }
            stuVos.add(stuVo);
        }
        if (stuVos.isEmpty()) {
            return AjaxResult.success("没有查询到\"" + queryStudent.getStudentName() + "\"同学的数据，请联系管理员！", stuVos);
        }
        return AjaxResult.success(stuVos);
    }

    /**
     * 获取部门树列表
     */
    @RequestMapping(value = "/deptTree", method = {RequestMethod.GET})
    public AjaxResult deptTree(SysDept dept) {
        List<TreeSelect> data = deptService.selectSchoolTreeList(dept);

        return AjaxResult.success(data);
    }
}
